<?php

namespace app\admin\model\wdsxh\mall;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'wdsxh_mall_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'is_recommend_text',
        'is_hot_text',
        'is_new_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getIsRecommendList()
    {
        return ['1' => __('Is_recommend 1'), '0' => __('Is_recommend 0')];
    }

    public function getIsHotList()
    {
        return ['1' => __('Is_hot 1'), '0' => __('Is_hot 0')];
    }

    public function getIsNewList()
    {
        return ['1' => __('Is_new 1'), '0' => __('Is_new 0')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }


    public function getIsRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_recommend']) ? $data['is_recommend'] : '');
        $list = $this->getIsRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_hot']) ? $data['is_hot'] : '');
        $list = $this->getIsHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsNewTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_new']) ? $data['is_new'] : '');
        $list = $this->getIsNewList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function singlecategory()
    {
        return $this->belongsTo('app\admin\model\wdsxh\single\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function singlepostage()
    {
        return $this->belongsTo('app\admin\model\wdsxh\single\PostageRules', 'freight_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getImageAttr($value,$data)
    {
        return wdsxh_full_url($value);
    }

    public function getRecommendImageAttr($value,$data)
    {
        return wdsxh_full_url($value);
    }
}
